module.exports = function (api) {
  api.cache(true);
  const { FIG_ENV } = process.env || {};
  const isProd = ['pre', 'prod'].includes(FIG_ENV);
  console.log('bable config');
  return {
    presets: [
      [
        '@babel/preset-env',
        {
          targets: {
            browsers: ['ie 11', 'last 2 version', '> 5%', 'not dead'],
          },
          modules: false,
          corejs: '3.29.1',
          useBuiltIns: 'usage', // entry根据浏览器不支持才引入兼容，usage仅检查es6以上兼容
        },
      ],
      '@babel/preset-typescript',
      '@babel/preset-react',
    ],
    plugins: [
      isProd && ['transform-remove-console', { exclude: ['info', 'error', 'warn'] }],
      [
        '@babel/plugin-transform-runtime',
        {
          corejs: 3,
          "absoluteRuntime": false,
          "helpers": true,
          "regenerator": true,
          proposals: true,
        },
      ],
      [
        '@babel/plugin-proposal-decorators',
        {
          legacy: true,
        },
      ],
      '@babel/proposal-object-rest-spread',
      '@babel/plugin-syntax-dynamic-import', // 动态导入
      '@babel/plugin-syntax-import-meta',
      '@babel/plugin-proposal-function-sent', // 转换成es5
      ['@babel/plugin-proposal-class-properties', { loose: true }],
      ['@babel/plugin-proposal-private-methods', { loose: true }],
      ['@babel/plugin-proposal-private-property-in-object', { loose: true }],
      '@babel/plugin-proposal-export-namespace-from',
      '@babel/plugin-transform-destructuring', // 结构赋值
      '@babel/plugin-transform-arrow-functions', // 箭头函数
      [
        "@babel/plugin-transform-modules-commonjs",
        {
          "allowTopLevelThis": true
        }
      ]
    ].filter(Boolean),
    env: {
      production: {
        plugins: [
          'transform-react-remove-prop-types', // 把proTypes独立一个包
          '@babel/plugin-transform-react-inline-elements', // React.createElement转换更有效率的 object literals
          '@babel/plugin-transform-react-constant-elements', // 加快react元素协调速度，避免不必要的重新实例化
        ],
      },
      development: {
        plugins: ['react-hot-loader/babel'],
      },
    },
  };
};
